package com.demo104_二叉树的最大深度;

class TreeNode {
    int val;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }

}

/**
 * 属于从上往下遍历的过程
 *
 * 属于先序遍历的过程
 */
class Solution {
    public int maxDepth(TreeNode root) {
        int maxDepth=0;
        if (root==null){
            return maxDepth;
        }
        maxDepth++;
        int max=dfs(root,maxDepth);

        return max;
    }

    private int dfs(TreeNode root, int maxDepth) {
        if (root==null){
            return maxDepth;
        }
        int tempLeft=maxDepth;
        int tempRight=maxDepth;
        if (root.left!=null){
            tempLeft++;
            tempLeft=dfs(root.left,tempLeft);
        }
        if(root.right!=null){
            tempRight++;
            tempRight=dfs(root.right,tempRight);
        }
        return Math.max(tempLeft,tempRight);
    }
}